package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.impl.CategoryServiceImpl;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryServiceImpl categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.sava(dishDTO);

//        清理緩存數據
        String key = "dish_" + dishDTO.getCategoryId();
        cleanCache(key);

        return Result.success();
    }

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id批量或单独删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        log.info("菜品批量删除：{}", ids);
        dishService.deleteByIds(ids);

//        清掉所有選中的菜品緩存數據
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 通過id查詢菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        log.info("通過id查詢菜品：{}", id);
        DishVO dishVO = dishService.getById(id);

        return  Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO) {

        log.info("修改菜品：{}", dishDTO);
        dishService.update(dishDTO);

//        清掉所有選中的菜品緩存數據
        cleanCache("dish_*");
        return Result.success();
    }

    @GetMapping("/list")
    public Result selectByCategoryId(Long categoryId) {
        log.info("根據分類的id 查詢菜品：{}", categoryId);
        List<DishVO> dishVO = dishService.selectById(categoryId);

        return Result.success(dishVO);
    }

    /**
     * 根據菜品名字查詢菜品
     * @param name
     * @return
     */
    @GetMapping("/list/{name}")
    public Result seleltByName(@PathVariable String name) {
        log.info("菜品名字查詢菜品：{}", name);

        List<DishVO> dishVO = dishService.getByName(name);

        return Result.success(dishVO);
    }


    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern) {
        //        清掉所有選中的菜品緩存數據
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);

    }

}
